package com.ruoyi.web.qualityController;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.net.HttpHeaders;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.quality.QualityIncomingMaterial;
import com.ruoyi.system.service.IQualityIncomingMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.io.InputStream;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/2/28 14:39
 */
@Api(tags = "来料检验单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Slf4j
@RequestMapping("/quality/material")
public class QualityIncomingMaterialController extends BaseController {

    @Autowired
    IQualityIncomingMaterialService iQualityIncomingMaterialService;

    /**
     * 查询来料检验单
     *
     * @param quality
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('system:material:list')")
    @ApiOperation(value = "查询来料检验单")
    @PostMapping("/list")
    public TableDataInfo list(QualityIncomingMaterial quality) {
        startPage();
        List<QualityIncomingMaterial> list = iQualityIncomingMaterialService.selectQualityProcessInspection(quality);
        return getDataTable(list);
    }

    @ApiOperation("删除过程检验单")
    @DeleteMapping("delete/id")
    public AjaxResult deleteList(Long id) {
        iQualityIncomingMaterialService.deleteQualityProcessInspectionById(id);
        return AjaxResult.success("删除成功！");
    }


    /**
     * 新增入库单
     */
    //@PreAuthorize("@ss.hasPermi('stores:warehouse:add')")
    @Log(title = "问题反馈", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增入库单")
    @PostMapping("/add")
    public AjaxResult add(@ApiParam(value = "新增问题反馈实体类", required = true) @RequestBody QualityIncomingMaterial OrdersManual) {
        return toAjax(iQualityIncomingMaterialService.insertQualityIncomingMaterial(OrdersManual));
    }




    @Autowired
    private com.ruoyi.web.controller.Upload.UploadController.UploadService uploadService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传")
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) throws IOException {
        // 调用 Service 层的方法保存图片
        if (file.getSize() < 0 || file.getSize() == 0) {
            throw new FileNotFoundException("没有文件");
        }
        String url = uploadService.save(file);
        QualityIncomingMaterial qualityIncomingMaterial = new QualityIncomingMaterial();
        qualityIncomingMaterial.setId(id);
        qualityIncomingMaterial.setUrl(url);
        iQualityIncomingMaterialService.insertQualityIncomingMaterial(qualityIncomingMaterial);
        // 返回图片 URL
        return url;
    }

    @ApiOperation("下载")
    @GetMapping("/download/image")
    public void downloadImage(@RequestParam("path") String imagePath, HttpServletResponse response) throws IOException {
        final File file = new File(imagePath);
        if (file.exists()) {
//            String contentType = URLConnection.guessContentTypeFromName(file.getName());
            final String suffix = imagePath.substring(imagePath.lastIndexOf(".") + 1);
            final String contentType = URLConnection.getFileNameMap().getContentTypeFor(".".concat(suffix));
//            response.setContentType(contentType);
            response.setContentType(contentType.concat(";charset=UTF-8"));
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
            response.setContentLength((int) file.length());
            try (final InputStream inputStream = Files.newInputStream(file.toPath())) {
                FileCopyUtils.copy(inputStream, response.getOutputStream());
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }


}
